import { MockMethod } from 'vite-plugin-mock';
import { successResult, errorResult, requestParams, getRequestToken } from '../mock/_result';
import { TokenPrefix } from '@/utils/auth';

export function createFakeUserList() {
    return [
        {
            user_id: '3306',
            user_name: 'admin',
            real_name: '扫地盲僧',
            avatar: 'https://api.multiavatar.com/blindmonk.svg',
            desc: '达摩深寺扫地僧，盲崖盘坐思人生',
            password: '123456',
            token: 'P1DeqWBao0HTU47Q',
            organization: '某大型公司CTO',
            location: '中国',
            routes: [
                {
                    name: 'home',
                    meta: {
                        title: '首页'
                    },
                    children: [
                        {
                            name: 'homeIndex'
                        }
                    ]
                },

                {
                    name: 'topTwo',
                    meta: {
                        title: '测试其他'
                    },
                    // redirect: '/top2LeftTwo',
                    children: [
                        {
                            name: 'tltchildOne',
                            meta: {
                                title: 'var控制'
                            }
                        },

                        {
                            name: 'top2LeftTwo',
                            meta: {
                                title: '测试--等'
                            },
                            children: [
                                {
                                    name: 't2ltchild1',
                                    meta: {
                                        title: '测试01'
                                    }
                                }
                            ]
                        }
                    ]
                },

                {
                    name: 'topOne',
                    meta: {
                        title: '测试换肤'
                    },
                    children: [
                        {
                            name: 'top1LeftTwo',
                            meta: {
                                title: '全局css变量控制'
                            },
                            children: [
                                {
                                    name: 't2ltchild2',
                                    meta: {
                                        title: '测试02--线上'
                                    }
                                }
                            ]
                        },
                        {
                            name: 'top2Left1',
                            meta: {
                                title: '伪类标签'
                            }
                        },
                        {
                            name: 'top1LeftOne',
                            meta: {
                                title: '测试变量v-bind绑定style'
                            }
                        }
                    ]
                }
            ]
        },
        {
            user_id: '80',
            user_name: 'test',
            real_name: '盲僧水友',
            avatar: 'https://api.multiavatar.com/test.svg',
            desc: '欢迎加入扫地盲僧水友群',
            password: 'test',
            token: 'yg8bE8nZwiCL4nQg',
            organization: '某大型公司CTO',
            location: '中国',
            routes: [
                {
                    name: 'topTwo',
                    meta: {
                        title: '测试其他'
                    },
                    redirect: '/top2LeftTwo',
                    children: [
                        {
                            name: 'tltchildOne',
                            meta: {
                                title: 'var控制'
                            }
                        },
                        {
                            name: 'top1LeftTwo',
                            meta: {
                                title: '全局css变量控制'
                            },
                            children: [
                                {
                                    name: 't2ltchild2',
                                    meta: {
                                        title: '测试02--线上'
                                    }
                                }
                            ]
                        },
                        {
                            name: 'top2LeftTwo',
                            meta: {
                                title: '测试--等'
                            },
                            children: [
                                {
                                    name: 't2ltchild1',
                                    meta: {
                                        title: '测试01'
                                    }
                                }
                            ]
                        }
                    ]
                },
                {
                    name: 'home',
                    meta: {
                        title: '首页'
                    },
                    children: [
                        {
                            name: 'homeIndex'
                        }
                    ]
                },
                {
                    name: 'topOne',
                    meta: {
                        title: '测试换肤'
                    },
                    children: [
                        {
                            name: 'top2Left1',
                            meta: {
                                title: '伪类标签'
                            }
                        },
                        {
                            name: 'top1LeftOne',
                            meta: {
                                title: '测试变量v-bind绑定style'
                            }
                        }
                    ]
                }
            ]
        }
    ];
}
export default [
    //登陆
    {
        url: '/login',
        timeout: 200,
        method: 'post',
        response: (request: requestParams) => {
            const { username, password } = request?.body;
            const checkUser = createFakeUserList().find(
                (item) => item.user_name === username && item.password === password
            );
            if (!checkUser) {
                return errorResult('不存在该用户');
            }
            return successResult(
                { token: checkUser.token, userInfo: checkUser },
                { message: '用户登录成功' }
            );
        }
    },
    //获取路由
    {
        url: '/getRoutes',
        timeout: 200,
        method: 'get',
        response: (request: requestParams) => {
            const token = getRequestToken(request);
            console.log(token);
            if (!token) return errorResult('Invalid token');
            const checkUser = createFakeUserList().find((item) => token.includes(item.token));
            if (!checkUser) {
                return errorResult('未获得相应的用户信息');
            }
            return successResult({ routes: checkUser.routes });
        }
    },
    //退出登录
    {
        url: '/logout',
        timeout: 200,
        method: 'post',
        response: (request: requestParams) => {
            console.dir(request);
            const token = getRequestToken(request);
            if (!token) return errorResult('token缺失!');
            const checkUser = createFakeUserList().find((item) => token.includes(item.token));
            if (!checkUser) {
                return errorResult('token缺失!');
            }
            return successResult('Token 已失效', { message: '退出登录啦' });
        }
    },
    {
        url: '/text',
        method: 'post',
        rawResponse: async (req, res) => {
            let reqbody = '';
            await new Promise((resolve) => {
                req.on('data', (chunk) => {
                    reqbody += chunk;
                });
                req.on('end', () => resolve(undefined));
            });
            res.setHeader('Content-Type', 'text/plain');
            res.statusCode = 200;
            res.end(`hello, ${reqbody}`);
        }
    }
] as MockMethod[];
